$(function() {
	var postId='';
	var repliedCustNo = '';
	var pageNum = 1;
	checkLogin();
	getMyMessage(pageNum);
	$('.l-more').click(function(){
		getMyMessage(++pageNum);
	});
	//clickFun();
})
var clickFun = function(){
	$('.act-area').click(function(){
		$('.act-area,.l-more').hide();
		$(this).show();
		$('.comment-txt').val('').show();
		$('.post-coment').show();
		var custName = $(this).find('h3').html();
		$('.comment-txt').attr('placeholder','回复 '+custName+':');
		postId = $(this).find('.review-pic').attr('id');
		repliedCustNo = $(this).attr('id');
	});
	$('.comment-txt').on('input propertychange',function(){
		if($.trim($(this).val())){

			$('.ops a').addClass('postit')
		}else{
			$('.ops a').removeClass('postit')
		}
	});
	$('.ops a').click(function(){
		if($(this).hasClass('postit')){
			console.log(postId,repliedCustNo);
			postComment(postId,$.trim($('.comment-txt').val()),repliedCustNo);//增加参数
		}
	})
}
var postComment = function(postId,comment,repliedCustNo){
	checkLogin(function(){
		$.ajax({
			url:baseurl+"/post/save/comment.do",
			type:'GET',
			dataType:'jsonp',
			data:{
				postId:postId,
				comment:comment,
				repliedCustNo:repliedCustNo
			}
		}).then(function(data){
			if(data.resultCode == 0){
				console.log('成功');
				AlertBox({type:'mini',alertCls:'alertBox',msg:'回复成功~~'});
				$('.comment-txt,.post-coment').hide();
				$('.act-area,.l-more').show();
			}else{
				console.log(data.resultMsg);
			}
		})
	})
}
var getMyMessage = function(pageNum){
	console.log(pageNum);
	$.ajax({
		url:baseurl + '/user/myMessage.do',
		type:'GET',
		dataType:'jsonp',
		data:{
			pageNum:pageNum||1
		}
	}).then(function(data){
		if(data.resultCode==0){
			
			var date = new Date(data.result.msgTime);
			$.each(data.result,function(index,item){
				var date = new Date(item.msgTime);
				item.msgTime = date.Format('yyyy-MM-dd hh:mm:ss');
			})
			console.log(data.result)
			if(data.result.length<21){
				$('.l-more').off('click').html('沒有更多了');
			}
			var html = template('tpl_actarea', data);
			if(pageNum>1){
				$('.mymes-list').append(html);
			}else{
				$('.mymes-list').html(html);
			}
			clickFun();
		}else{
			console.log(data.resultMsg);
		}
	})
}
//时间对象格式化
Date.prototype.Format = function (fmt) { 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}